<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px">

      <!--<el-form-item label="类型" prop="type">-->
        <!--<el-select style="width:180px;" v-model="dataForm.type" placeholder="类型">-->
          <!--<el-option label="client" :value="0"></el-option>-->
          <!--<el-option label="server" :value="1"></el-option>-->
        <!--</el-select>-->
      <!--</el-form-item>-->

      <el-form-item label="协议" prop="protocol">
        <el-select style="width:180px;" v-model="dataForm.protocol" placeholder="协议">
          <el-option label="http" :value="'http'"></el-option>
          <!--<el-option label="socks" :value="'socks'"></el-option>-->
        </el-select>
      </el-form-item>

      <el-form-item label="端口" prop="port">
        <el-input style="width:190px;" v-model="dataForm.port" placeholder="端口"></el-input>
      </el-form-item>

      <el-form-item label="账号" prop="username">
        <el-input style="width:190px;" v-model="dataForm.username" placeholder="username"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input style="width:190px;" v-model="dataForm.password" placeholder="password"></el-input>
      </el-form-item>

      <el-form-item label="启用" prop="status">
        <el-switch
          v-model="dataForm.status"
         >
        </el-switch>

      </el-form-item>

      <el-form-item label="白名单" prop="ips" >
        <el-select clearable multiple
                   v-model="dataForm.ips"
                   filterable
                   allow-create
                   remote
                   default-first-option
                   style="width:500px;">
          <el-option
            v-for="item in dataForm.ips"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
   import processConfigT from '@/api/core/processConfigT'
   import MultiUpload from "@/components/upload/multiUpload"
   let pk='';
  export default {
    components:{MultiUpload},
    data () {
      return {
        visible: false,
        tables:[],
     dataForm: {
          id: 0,
      type: 0,
      protocol: '',
      port: '',
      username: '',
      password: '',
      createTime: '',
       ips:[],
       open:true

    },

    dataRule: {
      protocol: [{ required: true, message: "协议不能为空", trigger: "blur" }],
      port: [{ required: true, message: "port不能为空", trigger: "blur" }],
      username: [{ required: true, message: "账号不能为空", trigger: "blur" }],
      password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        }
      }
    },
    methods: {
      init (id) {
        pk=id
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/processconfigt/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
              this.dataForm = data.processConfigT;
              }
            })
          }else{
            //新增的后端获取的未占用的端口和随机账号和密码
            this.$http({
              url: this.$http.adornUrl(`/sys/processconfigt/init`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.processConfigT;
              }
            })

          }
        })
      },




      // 表单提交
      dataFormSubmit () {
        if(this.dataForm.ips&&this.dataForm.ips.length>0){
          const pattern = /^(\d{1,3}\.){3}\d{1,3}$/;
          let invalidIps= this.dataForm.ips.filter((ip) => !pattern.test(ip))
          if(invalidIps&&invalidIps.length>0){
            this.$message.error("ip格式错误："+invalidIps.join(', '))
            return
          }
        }

        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/processconfigt/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
              'data': this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
